package cn.cmcc.online.smsapi.sdk.db.entity;

import java.util.HashMap;

import android.content.ContentValues;

import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.db.DBManager;
import cn.cmcc.online.smsapi.sdk.db.XyCursor;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
/*SDK-645 hewengao 20161019 start*/
public class JarSignManager {

    public static String getCreateTableSql() {
        String CREATE_TABLE = "create table  if not exists tb_jarsign(" + "id INTEGER PRIMARY KEY,"
                + "jarname TEXT unique," + "jarname_ver TEXT)";
        return CREATE_TABLE;
    }

    public static long insertOrUpdate(String jarName, String jarVer) {
        long res = 0;
        try {
            ContentValues values = new ContentValues();
            values.put("jarname", jarName);
            values.put("jarname_ver", jarVer);
            String tableName = "tb_jarsign";
            int rows = DBManager.update(tableName, values, "jarname=?", new String[] { jarName });
            if (rows <= 0) {
                res = DBManager.insert(tableName, values);
            }
        } catch (Throwable e) {
            LogManager.w(Constant.TAG, "JarSignManager insertOrUpdate error", e);
        }
        return res;
    }

    public static HashMap<String, Boolean> queryAllData() {
        HashMap<String, Boolean> res = null;
        XyCursor cursor = null;
        try {
            cursor = DBManager.query("tb_jarsign", new String[] { "jarname", "jarname_ver" }, null, null);
            if (cursor == null) {
                return null;
            }
            res = new HashMap<String, Boolean>();
            while (cursor.moveToNext()) {
                String key = cursor.getString(0) + cursor.getString(1);
                res.put(key, true);
            }
        } catch (Throwable e) {
            LogManager.w(Constant.TAG, "JarSignManager queryAllData error", e);
        } finally {
            XyCursor.closeCursor(cursor, true);
        }
        return res;
    }
}
/*SDK-645 hewengao 20161019 end*/
